package com.example.dao;


import com.example.domain.t_article;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface articleMapper {
    //超级管理员
    @Select("select * from t_article order by id DESC")
    public List<t_article> selectAll();
    //查询全部
    @Select("select * from t_article where secret=false  order by id DESC")
    public List<t_article> selectGetAll();
    @Select("select * from t_article where secret=false or user_id=#{user_id} order by id DESC")
    public List<t_article> selectGetAllByUser_id(int user_id);

    //查询某用户全部文章
    @Select("select * from t_article " +
            "where user_id=#{user_id} order by id DESC")
    public List<t_article> selectGetUserAll(int user_id);
    //根据id查询文章
    @Select("select * from t_article where id=#{id}")
    public t_article selectArticleById(int id);
    //根据id查询某用户的文章
    @Select("select * from t_article where id=#{id} and user_id=#{user_id}")
    public t_article selectArticleByUserId(int id,int user_id);

    //分页(模糊)查询
    @Select("select * from t_article " +
            "order by id DESC " +
            "limit #{count},#{size}")
    public List<t_article> selectArticles(int count,int size);
    @Select("select * from t_article " +
            "where title like #{like} or " +
            "content like #{like} or " +
            "categories like #{like} or " +
            "tags like #{like} " +
            "order by id DESC " +
            "limit #{count},#{size}")
    public List<t_article> selectLikeArticlesPage(String like,int count,int size);
    @Select(" select * from t_article " +
            " where title like #{like} and  secret=false " +
            " union " +
            " select * from t_article " +
            " where ( categories like #{like} or " +
            " tags like #{like} ) and  secret=false " +
            " union " +
            " select * from t_article " +
            " where  content like   #{like} and  secret=false ")
    public List<t_article> selectLikeArticles(String like);
    @Select(" select * from t_article " +
            " where title like #{like} " +
            " union " +
            " select * from t_article " +
            " where ( categories like #{like} or " +
            " tags like #{like} ) " +
            " union " +
            " select * from t_article " +
            " where  content like   #{like} ")
    public List<t_article> selectRootLikeArticles(String like);
    @Select(" select * from t_article " +
            " where title like #{like} and user_id=#{user_id} " +
            " union " +
            " select * from t_article " +
            " where (categories like #{like} or " +
            " tags like #{like} ) and user_id=#{user_id}" +
            " union " +
            " select * from t_article " +
            " where  content  like #{like} and user_id=#{user_id}")
    public List<t_article> selectLikeUserArticles(String like,int user_id);
    @Select(" select * from t_article " +
            " where title like #{like} and (user_id=#{user_id} or secret=false) " +
            " union " +
            " select * from t_article " +
            " where (categories like #{like} or " +
            " tags like #{like} ) and (user_id=#{user_id} or secret=false) " +
            " union " +
            " select * from t_article " +
            " where  ( content  like #{like}) and (user_id=#{user_id} or secret=false)")
    public List<t_article> selectLikeUserArticlesAndAll(String like,int user_id);

    //插入（发表文章）
    @Insert("insert into t_article(id,title,content,created,modified,categories,tags," +
            "allow_comment,thumbnail,user_id,secret) values(null,#{title},#{content},#{created}," +
            "#{modified},#{categories},#{tags},#{allow_comment},#{thumbnail},#{user_id},#{secret})")
    public boolean saveArticles(t_article art);

    //删除
    @Delete("delete from t_article where id=#{id}")
    public boolean deleteById(int id);

    //求总和
    @Select("select count(*) from t_article")
    public int countArticle();
    //用户的求总和
    @Select("select count(*) from t_article where user_id=#{user_id}")
    public int countArticleUser(int user_id);
    @Select("select count(*) from t_article " +
            "where title like #{like} or " +
            "content like #{like} or " +
            "categories like #{like} or " +
            "tags like #{like}")
    public int countLikeArticle(String like);
    //更新文章,使用ArticleMapper.xml配置该方法
//    @Select("UPDATE t_article SET title=#{title}," +
//            "content=#{content},created=#{created}," +
//            "modified=#{modified},categories=#{categories}," +
//            "tags=#{tags},allow_comment=#{allow_comment}," +
//                    "thumbnail=#{thumbnail} where id=#{id})")
    public boolean updateById(t_article art);

    //分类查询
    //查询所有的分类数
    @Select(" SELECT DISTINCT categories FROM t_article")
    public List<String> getAllCategories();

    @Select(" SELECT DISTINCT categories FROM t_article where user_id=#{user_id}")
    public List<String> getuserAllCategories(int user_id);
    //按照分类查询文章
    @Select(" SELECT * FROM t_article where categories=#{cate}")
    public List<t_article> getListByCategories(String cate);

    //按照分类查询用户文章
    @Select("SELECT * FROM t_article where categories=#{cate} and user_id=#{user_id}")
    public List<t_article> getuserListByCategories(String cate,int user_id);

    //分类查询（用户）文章
    public List<t_article> selectByUserArticles(t_article article);
    public List<t_article> selectByArticles(t_article article);


    @Update("update t_article set categories=#{news} where categories like #{old} and user_id=#{user_id}")
    public boolean updateCateByUser_id(String old,String news,int user_id);

}
